package top.sdaily.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.ServletWebArgumentResolverAdapter;
import top.sdaily.web.spring.interceptor.SecurityInterceptor;
import top.sdaily.web.spring.resolver.CustomerArgumentResolver;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * Created by soya on 2016/10/30.
 */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    Logger logger = LoggerFactory.getLogger(WebConfig.class);

    @Value("${img.path.root}")
    private String imgRootPath;
    @Value("${img.path.mapping-rule}")
    private String mappingRule;

    private static String[] resourceHandlers;
    private static String[] resourceLocations;

    @PostConstruct
    public void run(){
        logger.info("======================");
        logger.info("加载图片目录映射");
        logger.info("======================");
        String[] split = mappingRule.split(";");
        resourceHandlers = new String[split.length];
        resourceLocations = new String[split.length];
        for (int i = 0; i < split.length; i++){
            String[] split1 = split[i].split(":");
            resourceHandlers[i] = split1[0];
            resourceLocations[i] = "file:" + imgRootPath + split1[1];
        }
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);

        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat
        );
        fastConverter.setFastJsonConfig(fastJsonConfig);

        converters.add(fastConverter);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/**");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new ServletWebArgumentResolverAdapter(new CustomerArgumentResolver()));
    }

    /**
     * 配置外部图片目录及请求路径
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (resourceHandlers != null && resourceHandlers.length > 0) {
            registry.addResourceHandler(resourceHandlers)
                    .addResourceLocations(resourceLocations);
            super.addResourceHandlers(registry);
        }
    }
}
